package com.example.glideprogressiveloadimg.util

import java.util.*

/** Progressive JPEG config.  */
interface ProgressiveJpegConfig {
    /** Gets the next scan-number that should be decoded after the given scan-number.  */
    fun getNextScanNumberToDecode(scanNumber: Int): Int

    /** Gets the quality information for the given scan-number.  */
    fun getQualityInfo(scanNumber: Int): Int?
}


class SimpleProgressiveJpegConfig : ProgressiveJpegConfig {

    interface DynamicValueConfig {
        val scansToDecode: List<Int>
        val goodEnoughScanNumber: Int
    }

    private class DefaultDynamicValueConfig : DynamicValueConfig {
        override val scansToDecode: MutableList<Int>
            get() = MutableList(0, { 0 })

        override val goodEnoughScanNumber: Int
            get() = 0
    }

    private var mDynamicValueConfig: DynamicValueConfig? = null


    constructor() : this(DefaultDynamicValueConfig())

    constructor(dynamicValueConfig: DynamicValueConfig?) {
        mDynamicValueConfig = Preconditions.checkNotNull(dynamicValueConfig)
    }

    override fun getNextScanNumberToDecode(scanNumber: Int): Int {
        val scansToDecode: List<Int> = mDynamicValueConfig!!.scansToDecode
        if (scansToDecode == null || scansToDecode.isEmpty()) {
            return scanNumber + 1
        }
        for (i in scansToDecode.indices) {
            if (scansToDecode[i] > scanNumber) {
                return scansToDecode[i]
            }
        }
        return Int.MAX_VALUE
    }

    override fun getQualityInfo(scanNumber: Int): Int {
//        return ImmutableQualityInfo.of(
//            scanNumber,  /* isOfGoodEnoughQuality */
//            scanNumber >= mDynamicValueConfig.getGoodEnoughScanNumber(),  /* isOfFullQuality */
//            false
//        )
        return 0
    }
}